﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Security;
using GUILib;
using System.Windows;

namespace MetaTracker.ViewModel
{
    internal class RegisterWithingsViewModel : TrackerBaseViewModel, IRequestCloseViewModel
    {
        #region      Data and Properties

        //private SecureString _WithingsPassword;
        //public SecureString WithingsPassword
        //{
        //    get
        //    {
        //        return _WithingsPassword;
        //    }
        //    set 
        //    {
        //        _WithingsPassword = value;
        //        NotifyPropertyChanged("WithingsPassword");
        //    }
        //}

        //private string _WithingsEmail;
        //public string WithingsEmail
        //{
        //    get
        //    {
        //        return _WithingsEmail;
        //    }
        //    set
        //    {
        //        _WithingsEmail = value;
        //        NotifyPropertyChanged("WithingsEmail");
        //    }
        //}

        private string _WithingsPublicKey;
        public string WithingsPublicKey
        {
            get
            {
                return _WithingsPublicKey;
            }
            set
            {
                _WithingsPublicKey = value;
                NotifyPropertyChanged("WithingsPublicKey");
            }
        }

        private string _WithingsUserID;
        public string WithingsUserID
        {
            get
            {
                return _WithingsUserID;
            }
            set
            {
                _WithingsUserID = value;
                NotifyPropertyChanged("WithingsUserID");
            }
        }

        #endregion //Data and Properties

        #region      Constructors
        
        public RegisterWithingsViewModel()
        {
        }

        #endregion //Constructors

        #region      Commands

        //public ICommand GetUsersListCommand
        //{
        //    get
        //    {
        //        return Commands.GetOrCreateCommand(
        //            () => GetUsersListCommand,
        //            (parameter) => DoGetuserListCommand(parameter) ,
        //            () => true);
        //    }
        //}
        //private void DoGetuserListCommand(object parameter)
        //{

        //}


        public ICommand CancelCommand
        {
            get
            {
                return Commands.GetOrCreateCommand(
                    () => CancelCommand,
                    (parameter) => RequestClose(this, new DialogResultsEventArgs(null)),
                    () => true);
            }
        }

        public ICommand RegisterWithingsCommand
        {
            get
            {
                return Commands.GetOrCreateCommand(
                    () => RegisterWithingsCommand,
                    (parameter) => DoRegisterCommand(parameter),
                    () => !string.IsNullOrEmpty(WithingsUserID) && !string.IsNullOrEmpty(WithingsPublicKey));
            }
        }
        private void DoRegisterCommand(object parameter)
        {
            bool? result = tracker.RegisterWithingsWithPublicKey(WithingsUserID, WithingsPublicKey);
            if (result == null)
            {
                MessageBox.Show("User not found in the Withings database.  " + 
                    "Please copy/paste the UserID and Public Key again.",
                    "User not found!");
            }
            else if (result == false)
            {
                MessageBox.Show("This Public Key is incorrect, OR  this user does not have sharing settings enabled.  "
                    + "\nPlease check the \"Activate weight sharing\" box on the Withings site, and try again.",
                    "User is not sharing data!");
            }
            else if (result == true)
            {
                MessageBox.Show("Success!");
                RequestClose(this, new DialogResultsEventArgs(true));
            }
        }
        //private bool CanRegister()
        //{
        //    return !string.IsNullOrEmpty(WithingsUserID) && !string.IsNullOrEmpty(WithingsPublicKey);
        //}


        #endregion //Commands

        #region      Methods
        //internal bool Register()
        //{
        //    return tracker.RegisterWithings(WithingsEmail, WithingsPassword);
        //}








        #endregion //Methods



        public event EventHandler<DialogResultsEventArgs> RequestClose;
    }
}
